<template>
  <el-dialog
    v-model="props.visible"
    :footer="false"
    @close="emits('close')"
    width="800px"
  >
    <template #header="{ titleId, titleClass }">
      <div class="my-header flex">
        <h4
          :id="titleId"
          :class="titleClass"
        >
          <el-icon>
            <TakeawayBox />
          </el-icon>
          模板库
        </h4>
        <div class="search-box">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索模板"
            class="search-input"
          />
          <el-button
            link
            @click="handleSearch"
            class="search-button"
            :icon="Search"
          >
          </el-button>
        </div>
      </div>
    </template>
    <div
      class="dialog-content"
      v-show="show == 1"
    >
      <div class="empty-box">
        <div class="flex flex-wrap">
          <div
            class="empty-Folder flex-center w-120 h-120"
            @click="AddEmptyFile('add')"
          >
            <div class="empty-Folder-icon w-p-60">
              <FolderAdd />
            </div>
            <p>空白文档</p>
          </div>
          <el-upload
            :show-file-list="false"
            :auto-upload="false"
            ref="uploadRef"
            :on-change="onFileChange"
            :limit="1"
            accept=".docx"
          >
            <div
              class="empty-Folder flex-center w-120 h-120"
              >
              <!-- @click="AddEmptyFile('import')" -->
              <div class="empty-Folder-icon w-p-60">
                <FolderAdd />
              </div>
              <p>导入现有文档</p>
            </div>
          </el-upload>
          <!-- <div
            class="empty-Folder flex-center w-120 h-120"
            @click="AddEmptyFile('import')"
          >
            <div class="empty-Folder-icon w-p-60">
              <FolderAdd />
            </div>
            <p>导入现有文档</p>
          </div> -->
        </div>
      </div>

      <div class="basicTemplate-box">
        <h4>基础模板文件夹</h4>
        <div class="flex flex-wrap">
          <div
            v-for="(item) in basicTemplateList"
            :key="item.id"
          >
            <div class="empty-Folder flex-center w-120 h-120">
              <div class="empty-Folder-icon w-p-60">
                <FolderAdd />
              </div>
              <p>{{ item.folderName }}</p>
            </div>
            <div class="flex-center mt-10">
              <el-button
                type="info"
                size="small"
                class="btn"
                @click="previewFolder(item)"
              >
                进入
              </el-button>
              <!-- <el-button
                type="info"
                size="small"
                class="btn"
                @click="previewFolder(item)"
              >
                使用
              </el-button> -->
            </div>
          </div>
        </div>
      </div>
      <div class="myTemplate-box">
        <h4>我的模板文件夹</h4>
        <div v-show="!myTemplateList.length">-无数据-</div>
        <div
          v-show="myTemplateList.length"
          class="flex flex-wrap"
        >
          <div
            v-for="item in myTemplateList"
            :key="item.id"
          >
            <div class="empty-Folder flex-center w-120 h-120">
              <div class="empty-Folder-icon w-p-60">
                <FolderAdd />
              </div>
              <p class="_p">{{ item.folderName }}</p>
            </div>
            <div class="flex-center mt-10">
              <el-button
                type="info"
                size="small"
                class="btn"
                @click="previewFolder(item)"
              >进入</el-button>
              <!-- <el-button
                type="info"
                size="small"
                class="btn"
                @click="useTemplate(item, '2')"
              >使用</el-button> -->
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      class="dialog-content"
      v-show="show == 2"
    >
      <div class="folderInfo">
        <div class="folderNameBox">
          <div>{{ selectedTemplate?.folderName }}</div>
          <div>
            <el-button
              type="default"
              :icon="ArrowLeftBold"
              size="small"
              link
              @click="returnLastPage"
            >返回</el-button>
          </div>
        </div>
        <div class="remarkBox">
          {{
            selectedTemplate?.remark == ""
              ? "暂无备注"
              : selectedTemplate?.remark
          }}
        </div>
      </div>
      <div class="fileList flex flex-wrap">
        <div
          v-for="(item) in templateDocument"
          :key="item.id"
        >
          <div class="empty-Folder flex-center w-120 h-120">
            <div class="empty-Folder-icon w-p-60">
              <img class="icon_box" :src="menu_document" />
              <!-- <FolderAdd /> -->
            </div>
            <p class="_p">{{ item.fileName }}</p>
          </div>
          <div class="flex-center mt-10">
            <el-button
              type="info"
              size="small"
              class="btn"
              @click="previewFile(item)"
            >
              预览
            </el-button>
            <el-button
              type="info"
              size="small"
              class="btn"
              @click="useFolder(item)"
            >
              使用
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
  <FSPreFile :htmlStructure="htmlStructure" :visible="fullScreendialogVisible" @close="fullScreendialogVisible = false" />
</template>
<script setup>
import { Search, CirclePlus, ArrowLeftBold } from "@element-plus/icons-vue";
import { addFolder } from "@/api/folder";
import { getTemplateFolder,getMyTemplateList } from "@/api/template.js";
import { addDocument, getTemplateDocument, importDocument } from "@/api/document";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";
import menu_document from '@/assets/images/menu_document.png'
import FSPreFile from "./fullScreenPreviewFile.vue"
import { ref } from "vue";
const route = useRoute();
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  row: {
    type: Object,
    default: () => { },
  },
});
const emits = defineEmits(["close", "newCallBack"]);
const searchKeyword = ref("");
const uploadRef = ref(null);
const show = ref(1);
const selectedTemplate = ref(null);
const basicTemplateList = ref([]);
const myTemplateList = ref([]);
const templateDocument = ref([]);
const handleSearch = () => {
  console.log("handleSearch");
};
const AddEmptyFile = (type, templateFolderId = null) => {
  console.log("AddEmptyFile", route, type);
  // {
  //   creationMode: "0", // 0,1,2
  //   // "templateFolderId": "1" // 公共模板id
  //   // "templateFolderId": "1" // 我的模板id
  // }
  if (type === 'add') {
    let data = {
      remark: '备注',
      modelId: route.query?.modelId || '',
      fileName: templateFolderId ? '模板文档':'空白文档',
      folderId: route.query?.id || '',
      creationMode: templateFolderId ? '1':'0',
      templateDocumentId: templateFolderId
    }
    addDocument(data).then((res) => {
      console.log("resresresres", res);
      if (res.code == 200) {
        ElMessage({ message: "新建成功!", type: "success" });
        emits('close')
      }
    });
  } else {
    // importDocument
  }
};
const onFileChange = (data) => {
  console.log("dataonFileChange", data);
  let parmes = new FormData();

  parmes.append('file', data.raw)
  importDocument(route.query?.id || '', parmes).then((res) => {
    if (res.code == 200) {
      ElMessage({ message: "导入成功!", type: "success" });
      emits('close')
    }
  });
}
// 预览文件夹下的文件列表
const previewFolder = (item) => {
  console.log("previewFolder", item);
  selectedTemplate.value = item;
  // 获取文件夹详情和文件列表
  // dialogLoading.value = true;
  // getTemplateDocument(item.id).then(res => {
  //   console.log('模板文件夹下的文档模板列表', res);
  //   templateDocument.value = res.data;
  // });
  templateDocument.value = item.edPersonageTemplateDocumentVoList;
  show.value = 2;
};
const fullScreendialogVisible = ref(false);
const htmlStructure = ref("");
const previewFile = (item) => {
  console.log(item);
  htmlStructure.value = item.htmlStructure || '';
  // 打开全屏预览
  fullScreendialogVisible.value = true;
};
const returnLastPage = () => {
  show.value = 1;
};
const useFolder = (item) => {
  console.log("useFolder", item);
  AddEmptyFile('add', item.id)
};
onMounted(() => {
  console.log("NewFile");
  getTemplateFolder({ pageSize: 999, pageNum: 1 }).then((res) => {
    console.log(res, '获取基础模板');
    console.log(res);
    if (res.code == 200) {
      basicTemplateList.value = res.rows;
    }
  });
// 获取我的模板
getMyTemplateList().then((res) => {
    console.log(res);
    if (res.code == 200) {
      myTemplateList.value = res.data;
    }
  });
});
</script>
<style lang="scss" scoped>
.el-dialog__title {
  display: flex;
  align-items: center;
  gap: 10px;
}
.my-header {
  height: 34px;
  justify-content: space-between;
  .search-box {
    display: flex;
    align-items: center;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    margin-right: 15px;
    .search-input {
      flex: 1;
      border-right: 1px solid #dcdfe6;
      :deep(.el-input__wrapper) {
        box-shadow: none;
      }
      .el-input__icon {
        cursor: pointer;
        font-size: 18px;
        &:hover {
          color: #409eff; /* Element Plus 主色调 */
        }
      }
    }
    .search-button {
      height: 100%;
      padding: 0 8px;
    }
  }
}
.dialog-content {
  height: 60vh;
  overflow-y: auto;
  .flex.flex-wrap {
    gap: 20px;
    margin-top: 10px;
  }
  .folderInfo {
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    // height: 50px;
    padding: 10px;
    .folderNameBox {
      display: flex;
      justify-content: space-between;
    }
    .remarkBox {
      padding: 10px 10px 0;
      color: #999;
    }
  }
  .fileList {
    // box-sizing: border-box;
    padding: 20px;
    gap: 20px;
    .file {
      display: flex;
      flex-direction: column;
    }
  }
}
// .empty-box {
.empty-Folder {
  border: 1px solid #ccc;
  border-radius: 5px;
  flex-direction: column;
  padding: 10px;
  ._p{
    margin-top: 5px;
    margin-top: 5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
    text-align: center;
  }
}
.icon_box{
  width: 100%;
}
// }
.basicTemplate-box {
  margin: 30px 0;
  h4 {
    line-height: 50px;
  }
}
</style>
